<template>
    <div>
        <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item :to="{ path: '/' }">
                <i el-icon-s-promotion></i>订单管理
            </el-breadcrumb-item>
            <el-breadcrumb-item>添加订单</el-breadcrumb-item>
            <div style="float: right" >
                <a href="login" style="text-decoration: none;font-size: 20px;color: black">登录</a>
            </div>
        </el-breadcrumb>

        <el-divider/>

        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
            <el-form-item label="车辆ID" prop="carId">
                <el-input v-model="ruleForm.carId"></el-input>
            </el-form-item>
            <el-form-item label="订单状态" prop="status">
                <el-input v-model="ruleForm.status"></el-input>
            </el-form-item>
            <el-form-item label="订单类型" prop="type">
                <el-input v-model="ruleForm.type"></el-input>
            </el-form-item>
            <el-form-item label="订单主题" prop="theme">
                <el-input v-model="ruleForm.theme"></el-input>
            </el-form-item>
            <el-form-item label="订单项目" prop="name">
                <el-input v-model="ruleForm.name"></el-input>
            </el-form-item>
            <el-form-item label="负责人" prop="director">
                <el-input v-model="ruleForm.director"></el-input>
            </el-form-item>
            <el-form-item label="协作人" prop="tag">
                <el-input v-model="ruleForm.tag"></el-input>
            </el-form-item>
            <el-form-item label="预计交付时间" prop="oDelivery">
                <el-input v-model="ruleForm.oDelivery"></el-input>
            </el-form-item>

            <el-form-item>
                <el-button type="primary" @click="submitForm('ruleForm')">添加</el-button>
                <el-button @click="resetForm('ruleForm')">重置</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
    export default {
        data() {
            return {
                ruleForm: {
                    carId: '1',
                    status: '未开始',
                    type: '普通订单',
                    theme:'特斯拉',
                    name:'王小虎',
                    director:'程序猿',
                    tag:'B车',
                    oDelivery:'2023-01-01'
                },
              /**
               * required：true是必须填写的message：提示的消息     trigger：blur blur是焦点 丢失焦点就是光标不在
               */
                rules: {
                    carId: [
                        { required: true, message: '请输入车辆ID', trigger: 'blur' },
                        { min: 1, max: 10, message: '车辆ID长度必须在 1 到 10 个字符', trigger: 'blur' }
                    ],
                    status: [
                        { required: true, message: '请输入订单状态', trigger: 'blur' },
                        { min: 1, max: 10, message: '车辆ID长度必须在 1 到 10 个字符', trigger: 'blur' }
                    ],
                    type: [
                        { required: true, message: '请输订单类型', trigger: 'blur' },
                        { min: 1, max: 10, message: '车辆ID长度必须在 1 到 10 个字符', trigger: 'blur' }
                    ],
                    theme: [
                        { required: true, message: '请输订单主题', trigger: 'blur' },
                        { min: 1, max: 10, message: '车辆ID长度必须在 1 到 10 个字符', trigger: 'blur' }
                    ],
                    name: [
                        { required: true, message: '请输订单项目', trigger: 'blur' },
                        { min: 1, max: 10, message: '订单项目长度必须在 1 到 10 个字符', trigger: 'blur' }
                    ],
                    director: [
                        { required: true, message: '请输负责人', trigger: 'blur' },
                        { min: 2, max: 10, message: '负责人姓名长度必须在 2 到 10 个字符', trigger: 'blur' }
                    ],
                    tag: [
                        { required: true, message: '请输协作人', trigger: 'blur' },
                        { min: 2, max: 10, message: '协作人姓名长度必须在 2 到 10 个字符', trigger: 'blur' }
                    ],
                    oDelivery: [
                        { required: true, message: '预计交付时间', trigger: 'blur' },
                        { pattern:'^\\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\\d|3[01])$', message: '请输入正确时间格式yyyy-MM-dd', trigger: 'blur' }
                    ]
                }
            };
        },
        methods: {
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        let url = 'http://localhost:5081/order/add';
                        console.log('url = ' + url);

                        let formData = this.qs.stringify(this.ruleForm);
                        console.log('formData = ' + formData);

                        this.axios
                            .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
                            .post(url, formData).then((response) => {
                            let jsonResult = response.data;
                            if (jsonResult.state == 200) {
                                this.$message({
                                    message: '添加订单成功！',
                                    type: 'success'
                                });
                                this.resetForm(formName);
                            } else {
                                this.$alert(jsonResult.message, '操作失败', {
                                    confirmButtonText: '确定',
                                    callback: action => {}
                                });
                            }
                        });
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            },
            resetForm(formName) {
                this.$refs[formName].resetFields();
            }
        }
    }
</script>>